home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 1 / PC Actual CD 01.iso / f1 / func3d.arj / FUENTE / FUNCWIN.CP_ / FUNCWIN.CP
Encoding:
Text File  |  1994-05-10  |  12.0 KB  |  462 lines

  1. //
  2. //   Fichero: FUNCWIN.CPP
  3. //
  4. //   Versi≤n: 1.0
  5. //     Autor: Antonio M. EstΘvez Lorenzo
  6. // Prop≤sito: WinMain()
  7. //            Ventana principal de la aplicaci≤n 
  8. //
  9.  
  10. #include <windows.h>
  11. #include <owl.h>
  12. #pragma  hdrstop
  13.  
  14. #include <bwcc.h>
  15. #include "Child.h"
  16. #include "FuncDlg.h"
  17. #include "oleserv.h"
  18. #include "Ejemplos.h"
  19. #include "strings.h"
  20. #include "Mensajes.h"
  21. #include "Mensajes.ids"
  22.  
  23. HWND hWindPrev;
  24.  
  25. TDatosEcuPar tmpEcuPar=
  26.         {{"COS(A)COS(B)","COS(A)SEN(B)","SEN(A)",
  27.           "Ecuaciones paramΘtricas",
  28.           "-PI/2","PI/2", "0","2PI", "16","22",
  29.           "15","15","15", "0","0","0", "0","0","0"},
  30.          {1,0,0,0,0,0, 1, 1,0,"1/2","60", 0,0,1,0, "5", 1,0,0,0,
  31.           0,359,120, 0,359,120, 120,120, 135},
  32.          {"0","0","0", "1","1","1", "","",0, 1,0,0,0},
  33.           0, {ChildMinX, ChildMinY}};
  34.  
  35. static TDatosFunc2V tmpFunc2V=
  36.         {{"X^3-3X*Y^2",
  37.           "Funci≤n de 2 variables",
  38.           "-1","1", "-1","1", "20","20",
  39.           "10","10","10", "0","0","0", "0","0","0"},
  40.          {1,0,0,0,0,0, 1, 1,0,"1/2","60", 0,0,1,0, "5", 1,0,0,0,
  41.           0,359,120, 0,359,120, 120,120, 135},
  42.          {"0","0","0", "1","1","1", "","",0, 1,0,0,0},
  43.           0, {ChildMinX, ChildMinY}};
  44.  
  45. extern BOOL  HayMensaje;
  46. static DWORD MemoriaLibre;
  47.  
  48. char  ProgDirect[260];
  49. char  FichAyuda[260];
  50. char *SecWindow   = "Func3D";
  51. char *ItemCoord[5]= {"Coord X", "Coord Y", "Ancho", "Alto","Modo"};
  52. char *CoordInic[5]= {"20","20","600","400","0"};
  53.  
  54. BOOL PulsadaF1= FALSE;
  55.  
  56. BOOL NoEsAyuda(int idAyuda)
  57. {
  58.    if(idAyuda && PulsadaF1)
  59.    {
  60.       WinHelp(GetApplicationObject()->MainWindow->HWindow,
  61.               FichAyuda, HELP_CONTEXT, idAyuda);
  62.       return (PulsadaF1= FALSE);
  63.    }
  64.    return TRUE;
  65. }
  66.  
  67.  
  68. /* TProgWindow */
  69.  
  70. class TProgWindow : public TOLEWindMDI
  71. {
  72.    HICON Icono;
  73.    BOOL  HayEjemplos;
  74.    TWindow *Child;
  75.    TWindowsObject *WindCalc;
  76.    void GetDatIni();
  77.    void SetDatIni();
  78.    void NuevaSuperf(BOOL EsEcuPar, int Mensaje, BOOL embedded= FALSE,
  79.                LPVOID Datos= NULL, TSuperf **Sup= NULL, LPARAM pDoc= NULL);
  80. protected:
  81.    virtual void GetWindowClass(WNDCLASS& AWndClass);
  82.    virtual void Paint(HDC PaintDC, PAINTSTRUCT _FAR &PaintInfo);
  83.    virtual void SetupWindow();
  84.    virtual BOOL CanClose();
  85.    virtual PTWindowsObject InitChild();
  86.    void WMEnterIdle(RTMessage)      = [WM_FIRST + WM_ENTERIDLE];
  87.    void WMSize(RTMessage)           = [WM_FIRST + WM_SIZE];
  88.    void CMArrangeIcons(RTMessage)   = [CM_FIRST + CM_ARRANGEICONS];
  89.    void CMTileChildren(RTMessage)   = [CM_FIRST + CM_TILECHILDREN];
  90.    void CMCascadeChildren(RTMessage)= [CM_FIRST + CM_CASCADECHILDREN];
  91.    void CMCloseChildren(RTMessage)  = [CM_FIRST + CM_CLOSECHILDREN];
  92.    void SelecEcuPar(RTMessage)      = [CM_FIRST + cmEcuPar];
  93.    void SelecFunc2V(RTMessage)      = [CM_FIRST + cmFunc2V];
  94.    void Calculadora(RTMessage)      = [CM_FIRST + cmCalculadora];
  95.    void Ejemplos(RTMessage)         = [CM_FIRST + cmEjemplos];
  96.    void About(RTMessage)            = [CM_FIRST + cmAbout];
  97.    void Terminar(RTMessage)         = [CM_FIRST + cmTerminar];
  98.    void FuncDef(RTMessage)          = [CM_FIRST + cmFuncDef];
  99.    void Ayuda(RTMessage)            = [CM_FIRST + cmAyuda];
  100.    void IndAyuda(RTMessage)         = [CM_FIRST + cmIndAyuda];
  101.    void Setup(RTMessage)            = [ID_FIRST + cmSetup];
  102.    void FinEmbedded(RTMessage)      = [ID_FIRST + cmFinEmbedded];
  103.    void CopiaEjemplo(RTMessage)     = [ID_FIRST + cmCopiaEjemplo];
  104. public:
  105.    TProgWindow(LPSTR Titulo, PTModule Modulo= NULL);
  106.    void CreaChildWind(LPVOID Datos, LPOLESERVERDOC pDoc,
  107.                       TWindow **Wind, TSuperf **Sup);
  108.    void WriteMemor();
  109. };
  110.  
  111. TProgWindow::TProgWindow(LPSTR Titulo, PTModule Modulo)
  112.             :TOLEWindMDI(Titulo,"Menu",Modulo)
  113. {
  114.    ChildMenuPos= 2;
  115.    GetDatIni();
  116.    HayEjemplos= FALSE;
  117. }
  118.  
  119. void TProgWindow::SetupWindow()
  120. {
  121.    TOLEWindMDI::SetupWindow();
  122.  
  123.    hWindPrev= HWindow;
  124. }
  125.  
  126. void TProgWindow::GetWindowClass(WNDCLASS& AWndClass)
  127. {
  128.    TWindow::GetWindowClass(AWndClass);
  129.    AWndClass.style|= CS_BYTEALIGNCLIENT;
  130.    AWndClass.hIcon= Icono= LoadIcon(GetApplication()->hInstance,"Func3D");
  131. }
  132.  
  133. void TProgWindow::WMSize(RTMessage Msg)
  134. {
  135.    TMDIFrame::WMSize(Msg);
  136.  
  137.    HayMensaje= (Msg.WParam== SIZE_MINIMIZED);
  138. }
  139.  
  140. void TProgWindow::GetDatIni()
  141. {
  142.    char Fich[270];
  143.    char Num[10];
  144.    int i,*Coord= (int*)&Attr.X;
  145.  
  146.    StrUne(Fich,ProgDirect,"FUNCWIN.INI");
  147.    for(i= 0; i< 4; i++)
  148.    {
  149.       GetPrivateProfileString(SecWindow,ItemCoord[i],CoordInic[i],
  150.                               Num,sizeof(Num),Fich);
  151.       StToInt(Num,Coord++);
  152.    }
  153.    GetPrivateProfileString(SecWindow,ItemCoord[i],CoordInic[i],
  154.                            Num,sizeof(Num),Fich);
  155.    if(Num[0]== '1')
  156.       GetApplicationObject()->nCmdShow = SW_SHOWMAXIMIZED;
  157. }
  158.  
  159. void TProgWindow::SetDatIni()
  160. {
  161.    char Fich[270];
  162.    char Num[10];
  163.    int  i,*Coord= (int*)&Attr.X;
  164.  
  165.    StrUne(Fich,ProgDirect,"FUNCWIN.INI");
  166.    for(i= 0; i< 4; i++)
  167.    {
  168.       IntToSt(Num,*Coord++);
  169.       WritePrivateProfileString(SecWindow,ItemCoord[i],Num,Fich);
  170.    }
  171.    if(IsZoomed(HWindow))
  172.       Num[0]= '1';
  173.    else
  174.       Num[0]= '0';
  175.    Num[1]= '\0';
  176.    WritePrivateProfileString(SecWindow,ItemCoord[i],Num,Fich);
  177. }
  178.  
  179. BOOL TProgWindow::CanClose()
  180. {
  181.    TOLEProg   *Prog  = (TOLEProg*)GetApplicationObject();
  182.    TOLEServer *Server= Prog->pServer;
  183.  
  184.    if(OleRevokeServer(Server->lhServer)== OLE_WAIT_FOR_RELEASE)
  185.       Prog->Wait(Server->fRelease);
  186.    WinHelp(HWindow,FichAyuda,HELP_QUIT,NULL);
  187.    SetDatIni();
  188.    DestroyIcon(Icono);
  189.    return TRUE;
  190. }
  191.  
  192. void TProgWindow::FinEmbedded(RTMessage Msg)
  193. {
  194.    OleRevokeServerDoc(((TOLEDocument*)Msg.LParam)->lhDoc);
  195. }
  196.  
  197. void TProgWindow::Setup(RTMessage Msg)
  198. {
  199.    if(NoEsAyuda(cmSetup))
  200.       OleSavedServerDoc(((TOLEDocument*)Msg.LParam)->lhDoc);
  201. }
  202.  
  203. PTWindowsObject TProgWindow::InitChild()
  204. {
  205.    return Child;
  206. }
  207.  
  208. void TProgWindow::NuevaSuperf(BOOL EsEcuPar, int Mensaje, BOOL embedded,
  209.                               LPVOID Datos, TSuperf **Sup, LPARAM pDoc)
  210. {
  211.    if(EsEcuPar)
  212.    {
  213.       if(!Datos)
  214.          Datos= &tmpEcuPar;
  215.       Child= new TChildEcuPar(this, (TDatosEcuPar*)Datos, Sup, embedded, pDoc);
  216.    }
  217.    else
  218.    {
  219.       if(!Datos)
  220.          Datos= &tmpFunc2V;
  221.       Child= new TChildFunc2V(this, (TDatosFunc2V*)Datos, Sup, embedded, pDoc);
  222.    }
  223.    SendMessage(HWindow, WM_COMMAND, CM_CREATECHILD, NULL);
  224.    if(Mensaje)
  225.       SendMessage(Child->HWindow, WM_COMMAND, Mensaje, NULL);
  226. }
  227.  
  228. void TProgWindow::CreaChildWind(LPVOID Datos, LPOLESERVERDOC pDoc,
  229.                                 TWindow **Wind, TSuperf **Sup)
  230. {
  231.    BOOL EsEcuPar;
  232.    int  Mens= NULL;
  233.  
  234.    if(!Datos)
  235.    {
  236.       TTipoSup TipoSup;
  237.       TipoSup.EcuPar= TRUE;
  238.       TipoSup.Func2V= FALSE;
  239.       GetModule()->ExecDialog(new TDlgTipoSup(NULL,&TipoSup));
  240.       EsEcuPar= TipoSup.EcuPar;
  241.       Mens= cmEditar;
  242.    }
  243.    else
  244.       EsEcuPar= *((int*)Datos)++;
  245.  
  246.    NuevaSuperf(EsEcuPar, Mens, TRUE, Datos, Sup, (long)pDoc);
  247.    *Wind= Child;
  248. }
  249.  
  250. void TProgWindow::CopiaEjemplo(RTMessage Msg)
  251. {
  252.    NuevaSuperf(TRUE, cmDibujar, FALSE, (LPVOID)Msg.LParam);
  253. }
  254.  
  255. void TProgWindow::SelecEcuPar(RTMessage)
  256. {
  257.    if(NoEsAyuda(cmEcuPar))
  258.       NuevaSuperf(TRUE, cmEditar);
  259. }
  260.  
  261. void TProgWindow::SelecFunc2V(RTMessage)
  262. {
  263.    if(NoEsAyuda(cmFunc2V))
  264.       NuevaSuperf(FALSE, cmEditar);
  265. }
  266.  
  267. void TProgWindow::Terminar(RTMessage)
  268. {
  269.    if(NoEsAyuda(cmTerminar))
  270.       if(Mensaje(HWindow,MensTerminar,MensNombProg,
  271.                  MB_ICONQUESTION | MB_YESNO)== IDYES)
  272.          SendMessage(HWindow,WM_COMMAND,CM_EXIT,NULL);
  273. }
  274.  
  275. void TProgWindow::Calculadora(RTMessage)
  276. {
  277.    if(NoEsAyuda(cmCalculadora))
  278.    {
  279.       if(MF_GRAYED & GetMenuState(GetMenu(HWindow), cmCalculadora, MF_BYCOMMAND))
  280.       {
  281.          ShowWindow(WindCalc->HWindow, SW_SHOWNORMAL);
  282.          SendMessage(WindCalc->HWindow, WM_ACTIVATE, WA_CLICKACTIVE, NULL);
  283.       }
  284.       else
  285.          GetModule()->MakeWindow(WindCalc= new TDlgCalcula(this));
  286.    }
  287. }
  288.  
  289. void TProgWindow::FuncDef(RTMessage)
  290. {
  291.    if(NoEsAyuda(cmFuncDef))
  292.       GetModule()->ExecDialog(new TDlgFuncDef(this));
  293. }
  294.  
  295. void TProgWindow::Ejemplos(RTMessage)
  296. {
  297.    if(NoEsAyuda(cmEjemplos))
  298.    {
  299.       BOOL Hay= TRUE;
  300.       if(!HayEjemplos)
  301.       {
  302.          HayEjemplos= TRUE;
  303.          if(!TDlgEjemplos::HayEjemplos(ProgDirect))
  304.          {
  305.             HMENU Menu= GetMenu(HWindow);
  306.             EnableMenuItem(Menu, cmEjemplos, MF_BYCOMMAND | MF_GRAYED);
  307.             Hay= FALSE;
  308.          }
  309.       }
  310.       if(Hay)
  311.          GetModule()->ExecDialog(new TDlgEjemplos(this));
  312.    }
  313. }
  314.  
  315. void TProgWindow::About(RTMessage)
  316. {
  317.    if(NoEsAyuda(cmAbout))
  318.       GetModule()->ExecDialog(new TWindDlg(this,dgAbout));
  319. }
  320.  
  321. void TProgWindow::IndAyuda(RTMessage)
  322. {
  323.    PulsadaF1= FALSE;
  324.    WinHelp(HWindow,FichAyuda,HELP_CONTENTS,NULL);
  325. }
  326.  
  327. void TProgWindow::Ayuda(RTMessage)
  328. {
  329.    PulsadaF1= TRUE;
  330.    NoEsAyuda(hlpWindow);
  331. }
  332.  
  333. void TProgWindow::CMArrangeIcons(RTMessage Msg)
  334. {
  335.    if(NoEsAyuda(hlpMenuVentanas))
  336.       TOLEWindMDI::CMArrangeIcons(Msg);
  337. }
  338.  
  339. void TProgWindow::CMTileChildren(RTMessage Msg)
  340. {
  341.    if(NoEsAyuda(hlpMenuVentanas))
  342.       TOLEWindMDI::CMTileChildren(Msg);
  343. }
  344. void TProgWindow::CMCascadeChildren(RTMessage Msg)
  345. {
  346.    if(NoEsAyuda(hlpMenuVentanas))
  347.       TOLEWindMDI::CMCascadeChildren(Msg);
  348. }
  349. void TProgWindow::CMCloseChildren(RTMessage Msg)
  350. {
  351.    if(NoEsAyuda(hlpMenuVentanas))
  352.       TOLEWindMDI::CMCloseChildren(Msg);
  353. }
  354.  
  355. void TProgWindow::WMEnterIdle( RTMessage Msg )
  356. {
  357.    if((Msg.WParam == MSGF_MENU ) &&
  358.       ((GetKeyState( VK_F1 ) & 0x8000) != 0) )
  359.    {
  360.       PulsadaF1= TRUE;
  361.       PostMessage(HWindow, WM_KEYDOWN, VK_RETURN, NULL );
  362.    }
  363. }
  364.  
  365. void TProgWindow::Paint(HDC PaintDC, PAINTSTRUCT _FAR &PaintInfo)
  366. {
  367.    WriteMemor();
  368.    TOLEWindMDI::Paint(PaintDC, PaintInfo);
  369. }
  370.  
  371. void TProgWindow::WriteMemor()
  372. {
  373.    char buff[30];
  374.    HDC  Hdc = GetWindowDC(HWindow);
  375.    int  PosY= GetSystemMetrics(SM_CYCAPTION) +
  376.               GetSystemMetrics(SM_CYFRAME);
  377.    wsprintf(buff,"%lu bytes libres.",MemoriaLibre);
  378.    TextOut(Hdc,350,PosY,buff,LongStr(buff));
  379.    ReleaseDC(HWindow,Hdc);
  380. }
  381.  
  382.  
  383. /* TProgram */
  384.  
  385. class TProgram: public TOLEProg
  386. {
  387. protected:
  388.    virtual void InitInstance();
  389.    virtual void InitMainWindow();
  390.    virtual void IdleAction();
  391. public:
  392.    TProgram(LPSTR Nombre, HINSTANCE hInstance, HINSTANCE hPrevInstance,
  393.             LPSTR lpCmdLine, int nCmdShow)
  394.       : TOLEProg(Nombre, hInstance, hPrevInstance, lpCmdLine, nCmdShow) {};
  395. };
  396.  
  397. void CreaVariables();
  398.  
  399. void TProgram::InitMainWindow()
  400. {
  401.    BWCCGetVersion();
  402.    MainWindow= new TProgWindow(Name);
  403. }
  404.  
  405. void TProgram::InitInstance()
  406. {
  407.    TOLEProg::InitInstance();
  408.    HAccTable= LoadAccelerators(hInstance,MAKEINTRESOURCE(Acelerador));
  409.    CreaVariables();
  410.    SetupMenu();
  411.    if(!Embedded)
  412.       ExecDialog(new TWindDlg(NULL,dgAbout));
  413. }
  414.  
  415. void TProgram::IdleAction()
  416. {
  417.    DWORD Memor= GetFreeSpace(NULL);
  418.    if(Memor!= MemoriaLibre)
  419.    {
  420.       MemoriaLibre= Memor;
  421.       ((TProgWindow*)MainWindow)->WriteMemor();
  422.    }
  423. }
  424.  
  425.  
  426. /* WINMAIN */
  427.  
  428. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst,
  429.                    LPSTR lpCmdLine, int nCmdShow)
  430. {
  431.    if(hPrevInst)
  432.    {
  433.       asm{
  434.           mov   ax,hPrevInst
  435.           inc   ax
  436.           push  ds
  437.           mov   ds,ax
  438.           mov   ax,hWindPrev
  439.           pop   ds
  440.           mov   hWindPrev,ax
  441.       }
  442.       if(IsIconic(hWindPrev))
  443.          ShowWindow(hWindPrev, SW_RESTORE);
  444.       SetActiveWindow(hWindPrev);
  445.       return FALSE;
  446.    }
  447.  
  448.    char buff[270];
  449.    GetModuleFileName(hInstance,ProgDirect,sizeof(ProgDirect));
  450.    GetDirStr(ProgDirect);
  451.    StrUne(FichAyuda,ProgDirect,"FUNCWIN.HLP");
  452.    StrUne(buff,ProgDirect,"BORTE.FON");
  453.    AddFontResource(buff);
  454.    SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
  455.    TProgram Program("FUNC3D: Dibuja superficies en 3 dimensiones",
  456.                     hInstance, hPrevInst, lpCmdLine, nCmdShow);
  457.    Program.Run();
  458.    if(RemoveFontResource(buff))
  459.       SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
  460.    return Program.Status;
  461. }
  462.